package media;

import javax.sound.sampled.AudioFileFormat.Type;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import com.sun.speech.freetts.audio.AudioPlayer;
import com.sun.speech.freetts.audio.SingleFileAudioPlayer;


public class textToSpeech {
	  String voiceName = "kevin16";
	  String message = "";
	  VoiceManager voiceManager;
	  static Voice voice;
	  static String filePath = "currentmessage";
	  AudioPlayer audioPlayer;


	    
	  public textToSpeech(String fileName, String messageString){
		  int idx;
		  idx = fileName.indexOf(".wav");
		  
		  if(idx != -1){
			  filePath = fileName.substring(0,idx);
		  }
		  else{
			  filePath = fileName;
		  }
		  message = messageString;
		  audioPlayer = new SingleFileAudioPlayer(filePath, Type.WAVE);
		  this.setup(); 
	  }
	  
	  void setup() {
		  System.out.println("Using voice: " + voiceName);

		  voiceManager = VoiceManager.getInstance();
		  voice = voiceManager.getVoice(voiceName);	

		  voice.setPitch((float) 1.75);
		  voice.setPitchShift((float) 0.75);
		  voice.setPitchRange((float) 10.1); 
		  voice.setStyle("business");  //"business", "casual", "robotic", "breathy"

		  if (voice == null) {
			  System.err.println(
					  "Cannot find a voice named "
					  + voiceName + ".  Please specify a different voice.");
			  System.exit(1);
		   }	
		   voice.allocate();		  
	  }

	  void generateWav(String _a){
		  voice.setAudioPlayer(audioPlayer);
		  if(_a==null){
			  _a= "nothing"; 
		  }
		  voice.speak(_a);
	  }
		  
	  void exit(){			  
		  voice.deallocate(); 
		  audioPlayer.close();
	  }		  

	  public void record(){
		  generateWav(message);
		  exit();
	  }
}